var scrollTopRecord = 0;
var pageIndex = 2;
var mainCenterDivNum = 9;
$(function(){
	$("#txtAddress").live("focusin", function(){
		$("#txtAddress").val('');
	});
	$("#txtAddress").live("focusout", function(){
		var condition = $("#txtAddress").val();
		if(condition == ""){
			$("#txtAddress").val('请输入品牌');
		}
	});
	if($.cookie("collectYouleche") == null){
		$.cookie("collectYouleche", "");		
	}else{
		if($.cookie("collectYouleche") != ""){
			var shoucangNum = $.cookie("collectYouleche").split('^').length;
			$("#numShop").text(shoucangNum);
			$("#numShopSmall").text(shoucangNum);
		}
	}
	$(".daohangDiv").live("mouseover",function(){
		$(this).css("border-bottom","3px solid #FFF");
	});
	$(".daohangDiv").live("mouseout",function(){
		$(this).css("border-bottom","3px solid #3CAC7E");
	});
	
	$("#btnGouwuche").live("mouseover",function(){
		$(this).css("border-bottom","3px solid #FFF").css("border-right","3px solid #FFF").css("border-left","3px solid #FFF").css("border-radius", "5px;");
	});
	
	$("#btnGouwuche").live("mouseout",function(){
		$(this).css("border-bottom","1px solid #3CAC7E").css("border-right","2px solid #3CAC7E").css("border-left","2px solid #3CAC7E");
	});
	
	var offsetleft = document.getElementById("mainCenter").offsetLeft + 810;

	var top = $(window).scrollTop()+485; 
	var left= $(window).scrollLeft() + offsetleft + 40; 
	$("#gouwuche").css({ left:left + "px", top: top + "px" });
	$("#gouwuche").css({ left:left + "px", top: top + "px" });
	$(window).scroll(function() { 
		var top = $(window).scrollTop()+485; 
		var left= $(window).scrollLeft() + offsetleft + 40; 
		$("#gouwuche").css({ left:left + "px", top: top + "px" }); 
	}); 
	
	window.onscroll = function () {
	    var winScroll = document.documentElement.scrollTop || document.body.scrollTop;
	    if(winScroll > scrollTopRecord){
	    	if (winScroll >= (document.body.offsetHeight - document.documentElement.clientHeight - 400) && winScroll <= (document.body.offsetHeight - document.documentElement.clientHeight)) {
	 	       $.ajax({
	 	    	   type: "get",
	 	    	   dataType: "text",
	 	    	   url: "che.do?action=8&pageSize=10&pageIndex="+pageIndex,
	 	    	   success: function(v){
	 	    		   scrollTopRecord = winScroll;
	 	    		   pageIndex += 1;
	 	    		   if(v == ""){
	 	    			   layer.alert("已经没有车辆信息了", 9);
	 	    			   return;
	 	    		   }
	 	    		   
	 	    		   eval("var vehicles="+v);
		 	    	   $.each(vehicles, function(index, value){
		 	    		   $("#mainCenterDiv"+mainCenterDivNum).after(getHomeSigleVehicleInfoHtml(value.vehicleName,
		 	    				   value.A1,value.A2,value.vehicleId,value.batterySize,
		 	    				   value.electricitySize,value.addTime));
		 	    		   mainCenterDivNum += 1;
		 			   });
	 	    	   }
	 	       });
	 	    }
	    }	    
	};
});

function search(){
	var searchValue = $("#txtAddress").val();
	var args = "v=" + encodeURIComponent(searchValue);
	if(searchValue == "请输入品牌"){
		layer.alert("请输入品牌", 3);
		return;
	}
	
	$.ajax({
		type: "get",
		dataType: "text",
		url: "s.do?" + args +"&pageSize=10&pageIndex=1",
		success: function(v){
			if(v != ""){
				$("#mainCenter").html('');
			}else{
				$("#mainCenter").html('');
				$("#mainCenter").html('<br/><br/><br/><br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;亲迷，没有搜索到你想要的结果......<br/><br/><br/><br/><br/><br/>');
				return;
			}
			eval("var vehiclelist="+v);
    	   $.each(vehiclelist, function(index, value){
    		   $("#mainCenter").html(getHomeSigleVehicleInfoHtml(value.vehicleName,
    				   value.A1,value.A2,value.vehicleId,value.batterySize,
    				   value.electricitySize,value.addTime));
    		   mainCenterDivNum += 1;
		   });
		}
	});
}

function getHomeSigleVehicleInfoHtml(vehicleName,a1,a2,vehicleId,batterySize,electricitySize,addTime){
	var str = "<div class=\"mainCenterArea\" id=\"mainCenterDiv"+(mainCenterDivNum+1)+"\">";
	str += "<div class=\"mainCenterAreaImg\">";
	str += "<img src=\"image/fangwu.jpg\" />";
	str += "</div>";
	str += "<div class=\"mainCenterAreaDesc\">";
	str += "<div";
	str += "	style=\"float: left; width: 385px; color: #333333; font-weight: bold; font-size: 20px; height: 50px; margin: 5px 0px 0px 15px; vertical-align: middle;\">";
	str += "【"+vehicleName+"】"+a1+"</div>";
	str += "<div";
	str += " style=\"float: left; width: 85px; height: 50px; margin: 5px 0px 0px 15px; color: #47B689;\">";
	str += "[<a";
	str += " style=\"color: #47B689; font-size: 12px; text-decoration: underline;\">查看地图</a>]";
	str += "</div>";
	str += "<div";
	str += " style=\"text-align:right;float: left; width: 100px; height: 25px; font-weight: bold; color: #666; font-size: 14px;\">";
	str += "店铺地址：</div>";
	str += "<div";
	str += " style=\"float: left; width: 400px; height: 25px; color: #666;\">";
	str += ""+a2+"</div>";
	str += "<div";
	str += " style=\"text-align:right;float: left; width: 100px; height: 25px; font-weight: bold; color: #666; font-size: 14px;\">";
	str += "电池：</div>";
	str += "<div";
	str += " style=\"float: left; width: 200px; height: 25px; color: #666;\">";
	str += ""+batterySize+"V"+electricitySize+"A</div>";
	str += "<div style=\"float: right; width: 200px; height: 235px;\">";
	str += "<div";
	str += " style=\"float: left; width: 70px; height: 35px; text-align: right; font-size: 14px;\">发布时间：</div>";
	str += "<div";
	str += " style=\"float: left; width: 130px; height: 35px; text-align: left;\">"+dateFormat(addTime)+"</div>";
	str += "<div style=\"float: left; width: 200px; height: 140px;\">";
	str += "	<div";
	str += " style=\"float: left; width: 50px; height: 140px; text-align: right; line-height: 90px; vertical-align: text-top; font-size: 20px; font-style: italic; color: #444;\">￥</div>";
	str += "<div";
	str += " style=\"float: left; width: 150px; height: 140px; text-align: left; line-height: 110px; font-size: 50px; color: #F60;\">12</div>";
				
	str += "</div>";
	str += "<div style=\"float: left; width: 200px; height: 60px;\">";
	str += "<a class=\"detail-btn1\" onclick=\"contactSeller('20140506172006-1733552528-youleche-vehicleInfo-0001');\">联系店主</a>";
	str += "</div>";
	str += "</div>";
	str += "<div";
	str += " style=\"text-align:right;float: left; width: 100px; height: 25px; font-weight: bold; color: #666; font-size: 14px;\">";
	str += "蓄电行程：</div>";
	str += "<div";
	str += " style=\"float: left; width: 200px; height: 25px; color: #666;\">";
	str += "12km</div>";
	str += "<div";
	str += " style=\"text-align:right;float: left; width: 100px; height: 25px; font-weight: bold; color: #666; font-size: 14px;\">";
	str += "最大速度：</div>";
	str += "<div";
	str += " style=\"float: left; width: 200px; height: 25px; color: #666;\">";
	str += "12km/h</div>";
	str += "<div";
	str += " style=\"text-align:right;float: left; width: 100px; height: 25px; font-weight: bold; color: #666; font-size: 14px;\">";
	str += "赠送：</div>";
	str += "<div";
	str += " style=\"float: left; width: 200px; height: 100px; color: #666;\">";
	str += "雨衣、 充电器、锁</div>";
	str += "<div style=\"float: left; width: 300px; height: 50px\">";
	str += "<a class=\"detail-btn\" onclick=\"addShopVehicle('"+vehicleId+"');\">加入收藏车</a> <a";
	str += " class=\"detail-btn\" href=\"/youleche/che.do?id="+vehicleId+"&action=1\" target=\"_blank\">车辆详情</a>";
	str += "</div>";
	str += "</div>";
	str += "</div>";

	return str;

}

function forwardShop(){
	window.location.href="shoucang.jsp";
}

function addShopVehicle(vehicleId){
	var idArr = $.cookie("collectYouleche");
	if(idArr == "" || idArr == null){
		$.cookie("collectYouleche", vehicleId);
	}else{
		var cookiesValue = $.cookie("collectYouleche");
		if(cookiesValue.indexOf(vehicleId) < 0){
			var num = $("#numShop").text();
			$("#numShop").text(parseInt(num) + 1);
			$("#numShopSmall").text(parseInt(num) + 1);
			var cookiesId = vehicleId + "^" + $.cookie("collectYouleche");
			$.cookie("collectYouleche", cookiesId);			
		}
		
	}
}

function dateFormat(date){
	var formatDate = "";
	var month = date.split(' ')[0];
	var day = date.split(' ')[1];
	var year = date.split(' ')[2].replace(",","");
	switch(month){
		case "一月":
			month = "01";
			break;
		case "二月":
			month = "02";
			break;
		case "三月":
			month = "03";
			break;
		case "四月":
			month = "04";
			break;
		case "五月":
			month = "05";
			break;
		case "六月":
			month = "06";
			break;
		case "七月":
			month = "07";
			break;
		case "八月":
			month = "08";
			break;
		case "九月":
			month = "09";
			break;
		case "十月":
			month = "10";
			break;
		case "十一月":
			month = "11";
			break;
		case "十二月":
			month = "12";
			break;
	}
	
	day=day.replace(/,/ig,""); 
	if(day.length == 1){
		day = "0" + day;
	}
	
	formatDate = year + "-" + month + "-" + day;
	return formatDate;
}